<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    // 统一处理函数
    function formatDate(date) {
      const year = date.getFullYear().toString().padStart(4, '0');
      const month = (date.getMonth() + 1).toString().padStart(2, '0');
      const day = date.getDate().toString().padStart(2, '0');
      const hour = date.getHours().toString().padStart(2, '0');
      const minute = date.getMinutes().toString().padStart(2, '0');
      const second = date.getSeconds().toString().padStart(2, '0');

      return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
    }

    // 示例1：传入 Date 类型对象
    const inputDate1 = new Date('2022-05-06T12:34:56');
    const outputDate1 = formatDate(inputDate1);
    console.log(outputDate1); // 输出 "2022-05-06 12:34:56"

    // 示例2：传入 Unix 时间戳（单位为秒）
    const inputDate2 = 1651804800;
    const outputDate2 = formatDate(new Date(inputDate2 * 1000));
    console.log(outputDate2); // 输出 "2022-05-06 00:00:00"

    // 示例3：传入 ISO 8601 格式的字符串
    const inputDate3 = '2022-05-06T00:00:00.000Z';
    const outputDate3 = formatDate(new Date(inputDate3));
    console.log(outputDate3); // 输出 "2022-05-06 00:00:00"

  </script>
</body>

</html>